#pragma once
#include "resource.h"
#include "stdafx.h"
#include "CollidableObject.h"
#include "GameWorld.h"
#include "SpriteManager.h"
#include "AnimatedSprite.h"

class Collision
{
	public:
		CollidableObject *co1;
		CollidableObject *co2;
		float timeOfCollision;
		float startTimeOfXCollision;
		float startTimeOfYCollision;
		float endTimeOfXCollision;
		float endTimeOfYCollision;
		Collision()
		{
			co1 = new CollidableObject();
			co2 = new CollidableObject();
		}
		Collision(CollidableObject *c1, CollidableObject *c2)
		{
			co1 = c1;
			co2 = c2;
		}
		~Collision()
		{
			delete co1;
			delete co2;
		}
		void update(Game *game)
		{
			GameWorld *world = game->getWorld();
			SpriteManager *spriteManager = world->getSpriteManager();
			AnimatedSprite *player = spriteManager->getPlayer();	

			int incX = 0, incY = 0;
			if(!co2->isCurrentlyCollidable())
				return;
			if(player->getBoundingVolume()->getX() < co2->getBoundingVolume()->getX())	// obj 1 is on the left and obj 2 is on the right
			{				
				startTimeOfXCollision = player->getBoundingVolume()->getX() + 
													player->getBoundingVolume()->getWidth() - 
													co2->getBoundingVolume()->getX();
				startTimeOfXCollision /= -5; // Assuming object is moving 5 pixel per frame (Velocity is 5)
				if(startTimeOfXCollision <= 0) // Colliding
					incX = -5;
			}
			else
			{
				startTimeOfXCollision = co2->getBoundingVolume()->getX() + 
													co2->getBoundingVolume()->getWidth() - 
													player->getBoundingVolume()->getX();
				startTimeOfXCollision /= -5; // Assuming object is moving 5 pixel per frame (Velocity is 5)
				if(startTimeOfXCollision <= 0) // Colliding
					incX = 5;
			}

			
			

			if(player->getBoundingVolume()->getY() < co2->getBoundingVolume()->getY())	// obj 1 is on the top and obj 2 is at the bottom
			{				
				startTimeOfYCollision = player->getBoundingVolume()->getY() + 
													player->getBoundingVolume()->getHeight() - 
													co2->getBoundingVolume()->getY();
				startTimeOfYCollision /= -5; // Assuming object is moving 5 pixel per frame (Velocity is 5)
				if(startTimeOfYCollision <= 0) // Colliding
					incY = -5;
			}
			else
			{
				startTimeOfYCollision = co2->getBoundingVolume()->getY() + 
													co2->getBoundingVolume()->getHeight() - 
													player->getBoundingVolume()->getY();
				startTimeOfYCollision /= -5; // Assuming object is moving 5 pixel per frame (Velocity is 5)
				if(startTimeOfYCollision <= 0)	// Colliding
					incY = 5;
			}

			if(startTimeOfXCollision > startTimeOfYCollision)
				timeOfCollision = startTimeOfYCollision;
			else
				timeOfCollision = startTimeOfXCollision;

			if(timeOfCollision <= 0)
			{
				if(abs(startTimeOfYCollision) <= 5 && abs(startTimeOfXCollision) <= 5)
				{					
					if(abs(startTimeOfYCollision) <= 5)
						player->getPhysicalProperties()->incY(incY);				
					if(abs(startTimeOfXCollision) <= 5)
						player->getPhysicalProperties()->incX(incX);
				}
			}
  		}		
};